using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DreamBuilder.DictionaryAccess;



namespace DreamBuilder.UI.UserControls
{
    public class RefInput : EditItemBase
    {
        public override event VoucherItemSelectedEventHandler VoucherItemSelected;
        RefEditor refEditor = null;
        VoucherEditItemInfo editItemInfo = null;

        public RefInput(EditArgs editArgs)
        {
            if (editArgs.UserControl != null)
            {
                refEditor = (RefEditor)editArgs.UserControl.LoadControl("RefEditor.ascx");
            }
            editItemInfo = DictionaryAccessFacade.GetVoucherEditItemInfo(editArgs.ID, editArgs.ItemCode);
            InstantiateIn(editArgs);
        }
        //RefNameValue
        public override string Value
        {
            get
            {
                return this.refEditor.RefNameValue;
            }
        }
        public override Control Control
        {
            get
            {
                return refEditor;
            }
        }

        protected override void SetControlID()
        {
            FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(editItemInfo.TableCode, editItemInfo.FieldCode);
            refEditor.ID = "refput_" + editItemInfo.TableCode + "__" + editItemInfo.FieldCode;
            string sReferenceObj = fieldDicInfo.ReferenceObj;
            refEditor.TableCode = sReferenceObj;


        }

        protected override void SetControlValue(EditArgs editArgs)
        {

            string sResult = "";
            DataTable dt = editArgs.EditData;
            string columnName = editItemInfo.TableCode + "__" + editItemInfo.FieldCode;
            if (dt.Rows.Count > 0 && dt.Rows[0][columnName] != System.DBNull.Value)
            {
                sResult = dt.Rows[0][columnName].ToString();
            }
            if (sResult.Trim().Length > 0)
            {
                refEditor.RefNameValue = sResult;
            }
            else
            {
                switch (editItemInfo.InitValue.ToLower())
                {
                    case "[today]":
                        refEditor.RefNameValue = DateTime.Now.ToString(@"yyyy-MM-dd");
                        break;
                    case "[now]":
                        refEditor.RefNameValue = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss");
                        break;
                    default:
                        refEditor.RefNameValue = editItemInfo.InitValue;
                        break;
                }
            }
            if (!string.IsNullOrEmpty(editItemInfo.RefRangeSql))
            {
                refEditor.WhereSql = refEditor.WhereSql == "" ? editItemInfo.RefRangeSql : ("(" + editItemInfo.RefRangeSql + ") and (" + refEditor.WhereSql + ")");
            }
        }
        protected override void SetControlAttributes(EditArgs editArgs)
        {
            refEditor.Required = (!editItemInfo.Nullable);
            refEditor.Enabled = (!editItemInfo.ReadOnly);
            refEditor.MultiSelect = 1;
            refEditor.ReadOnly = false;
            if (editItemInfo.AutoPostBack == true)
            {
                refEditor.RefSelected += new RefSelectHandler(pRefEditor_RefSelected);
            }
            refEditor.AutoPostBack = editItemInfo.AutoPostBack;
            refEditor.Width = Unit.Percentage(100);


        }
        private void pRefEditor_RefSelected(object sender, EventArgs e)
        {
            if (VoucherItemSelected != null)
            {
                VoucherItemSelected(refEditor, e);
            }
        }

    }
}